package com.magenta.maxoptra.gis.distancematrix.commands;

import com.magenta.maxoptra.gis.distancematrix.*;

import java.util.List;

/**
 * @author Sergey Grachev
 */
final class SynchronizeMatrices extends AbstractGroupCommand {

    private final List<MatrixCoordinates> matrices;
    private boolean requireMatrixUpdating;

    SynchronizeMatrices(final long groupId, final List<MatrixCoordinates> matrices) {
        super(groupId);
        this.matrices = matrices;
    }

    @Override
    public CommandType getType() {
        return CommandType.SYNCHRONIZE_MATRICES;
    }

    @Override
    public void execute(final DistanceMatrixDAO db, final DistanceMatrixServiceProxy service) {
        requireMatrixUpdating = db.synchronizeCoordinates(groupContext.getId(), matrices);
    }

    @Override
    public UpdateRequestType isRequireMatrixUpdating() {
        return requireMatrixUpdating ? UpdateRequestType.SCHEDULE : UpdateRequestType.NONE;
    }

    @Override
    public boolean isRequireGroupLock() {
        return true;
    }

    @Override
    public String toString() {
        return "SynchronizeMatrices{" +
                "groupId=" + groupId +
                ", matrices=" + matrices +
                '}';
    }
}
